package Entities;

import mysqlCnnection.CatalogueUpdate;

public class Product
{
	private double price;
	private String name;
	private String description;
    private long cataloguenumber;
	
    /**
	 * @param price
	 * @param name
	 * @param description
	 * @param catalougenumber
	 */
	public Product(double price, String name, String description, long cataloguenumber) {
		super();
		this.price = price;
		this.name = name;
		this.description = description;
		this.cataloguenumber = cataloguenumber;
	}

	/**
	 * @return the cataloguenumber
	 */
	public long getCataloguenumber() {
		return cataloguenumber;
	}

	/**
	 * @param cataloguenumber the cataloguenumber to set
	 */
	public void setCataloguenumber(long cataloguenumber) {
		this.cataloguenumber = cataloguenumber;
	}

	/**
	 * @return the price
	 */
	public double getPrice() {
		return price;
	}

	/**
	 * @param price the price to set
	 */
	public void setPrice(double price) {
		this.price = price;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * This function insert this product to data base
	 */
	public boolean insertToDataBase()
	{
		try
		{
			CatalogueUpdate catalogueupdate = new CatalogueUpdate();
			return (catalogueupdate.insertProduct(price, name, description, cataloguenumber));
		}		
		catch (Exception except)
		{
			return false;
		}	
	}
	
	/**
	 * This function load product (to this) from data base
	 */
	public boolean loadFromDataBase(long cataloguenum)
	{
		try 
		{
			CatalogueUpdate catalogueupdate = new CatalogueUpdate();
			String[] arrofstring = catalogueupdate.searchForProduct(cataloguenum);
			if 	(arrofstring != null)
			{
				setPrice(Double.parseDouble(arrofstring[0]));
				setName(arrofstring[1]);
				setDescription(arrofstring[2]);
				setCataloguenumber(Long.parseLong(arrofstring[3]));
				return true;
			}
			return false;
		}catch(Exception excp)
		{
			return false;			
		}			
	}
	
	/**
	 * This method update the data base to product new details 
	 * @return true on success, else false
	 */
	public boolean updateProduct()
	{
		try 
		{
			CatalogueUpdate catalogueupdate = new CatalogueUpdate();
			return (catalogueupdate.updateProduct(price, name, description, cataloguenumber));
		}catch (Exception except)
		{
			return false;
		}		
	}

	/**
	 * This method return string[] of product details 
	 */
	public String[] toArrString()
	{
		try
		{
			String[] productarr = new String[4];
			productarr[0] =  String.valueOf(getPrice());
			productarr[1] = this.getName();
			productarr[2] = this.getDescription();
			productarr[3] = String.valueOf(getCataloguenumber());
			return productarr;
		}catch(Exception e)
		{
			return null;
		}
	}
}
